package com.sskj.aikeeper.hangqing.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.gyf.barlibrary.ImmersionBar;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.sskj.aikeeper.R;
import com.sskj.aikeeper.bean.SignStatusBean;
import com.sskj.aikeeper.presenter.SignInPresenter;
import com.sskj.common.base.App;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.TimeFormatUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.SPConfig;
import com.sskj.lib.adapter.BaseAdapter;
import com.sskj.lib.adapter.ViewHolder;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.SignInBean;
import com.sskj.lib.bean.SignInListBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;

/**
 * @author Hey
 * Create at  2019/06/25
 */
@Route(path = RConfig.APP_ACTIVITY_SIGNIN)
public class SignInActivity extends BaseActivity<SignInPresenter> {
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.iv_signIn)
    ImageView ivSignIn;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.smartRefreshView)
    SmartRefreshLayout smartRefreshView;
    private int page = 1;
    private int size = 20;
    private SignInListBean data;
    private BaseAdapter<SignInListBean.ListBean> adapter;

    @Override
    public int getLayoutId() {
        return R.layout.app_activity_sign_in;
    }

    @Override
    public SignInPresenter getPresenter() {
        return new SignInPresenter();
    }

    @Override
    public void initView() {
        setState();
        ImmersionBar.with(this).statusBarColor(R.color.lib_theme).init();
        ImmersionBar.with(this).statusBarDarkFont(false).init();
        tvTitle.setText(App.INSTANCE.getString(R.string.app_signInActivity1));

        smartRefreshView.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.getSignInList(false, page, size);
        });
        smartRefreshView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                page++;
                mPresenter.getSignInList(false, page, size);
            }
        });

    }

    private void setState() {
        mPresenter.getSignInState();
        mPresenter.getSignInList(true, page, size);
    }

    @Override
    public void initData() {
        ClickUtil.click(300, ivSignIn, () -> {
            mPresenter.clickSignIn();
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new BaseAdapter<SignInListBean.ListBean>(R.layout.app_item_signin, null, recyclerView, true) {
            @Override
            public void bind(ViewHolder holder, SignInListBean.ListBean item) {
                holder.setText(R.id.tv_date, TimeFormatUtil.formatE(Long.valueOf(item.getAddtime()) * 1000))
                        .setText(R.id.tv_memo, "+" + item.getPrize_nums() + item.getPname().toUpperCase());
            }
        };
        View headView = LayoutInflater.from(this).inflate(R.layout.app_view_header_signin, null);
        adapter.addHeaderView(headView);
    }

    public void getSignInState(SignStatusBean data) {
        if (data.getStatus().equals("0")) {
            if (Constans.LANGUAGE_EN.equals(SPUtil.get(SPConfig.LANGUAGE, Constans.LANGUAGE_CNA))) {
                ivSignIn.setImageResource(R.mipmap.btn_weiqiandao_english);
            } else if (Constans.LANGUAGE_CNA.equals(SPUtil.get(SPConfig.LANGUAGE, Constans.LANGUAGE_CNA))) {
                ivSignIn.setImageResource(R.mipmap.btn_weiqiandao);
            } else if (Constans.LANGUAGE_TRA.equals(SPUtil.get(SPConfig.LANGUAGE, Constans.LANGUAGE_CNA))) {
                ivSignIn.setImageResource(R.mipmap.btn_weiqiandao_fan);
            }
        } else if (data.getStatus().equals("1")) {
            if (Constans.LANGUAGE_EN.equals(SPUtil.get(SPConfig.LANGUAGE, Constans.LANGUAGE_CNA))) {
                ivSignIn.setImageResource(R.mipmap.btn_yiqiandao_english);
            } else if (Constans.LANGUAGE_CNA.equals(SPUtil.get(SPConfig.LANGUAGE, Constans.LANGUAGE_CNA))) {
                ivSignIn.setImageResource(R.mipmap.btn_yiqiandao);
            } else if (Constans.LANGUAGE_TRA.equals(SPUtil.get(SPConfig.LANGUAGE, Constans.LANGUAGE_CNA))) {
                ivSignIn.setImageResource(R.mipmap.btn_yiqiandao_fan);
            }
        }
    }

    public void clickSignIn(int status) {
        if (status == 200) {
            mPresenter.getSignInList(false, page, size);
            ivSignIn.setImageResource(R.mipmap.btn_yiqiandao);
        }
    }

    @SuppressLint("CheckResult")
    public void getSignInList(SignInListBean data) {
        this.data = data;
        if (data == null) {
            return;
        }
        if (page == 1) {
            adapter.setNewData(data.getList());
        } else {
            adapter.addData(data.getList());
        }
    }

    public void stopRefresh() {
        if (smartRefreshView != null) {
            smartRefreshView.finishRefresh();
            smartRefreshView.finishLoadMore();
        }
    }
}
